export function formatTimestamp(timestamp) {
    // 处理秒级时间戳（10位）
    if (typeof timestamp === 'number' && timestamp < 1e12) {
        timestamp *= 1000;
    }
    
    const date = new Date(timestamp);
    const now = new Date();
    
    // 今天判断（年月日相同）
    if (isToday(date, now)) {
        return `今天 ${formatTime(date)}`;
    }
    
    // 昨天判断（当前日期-1天相同）
    if (isYesterday(date, now)) {
        return `昨天 ${formatTime(date)}`;
    }
    
    // 根据年份决定日期格式
    const includeYear = date.getFullYear() !== now.getFullYear();
    return `${formatChineseDate(date, includeYear)} ${formatTime(date)}`;
}

// 辅助函数：判断是否是今天
function isToday(date, now) {
    return date.getFullYear() === now.getFullYear() &&
        date.getMonth() === now.getMonth() &&
        date.getDate() === now.getDate();
}

// 辅助函数：判断是否是昨天
function isYesterday(date, now) {
    const yesterday = new Date(now);
    yesterday.setDate(now.getDate() - 1);
    return date.getFullYear() === yesterday.getFullYear() &&
        date.getMonth() === yesterday.getMonth() &&
        date.getDate() === yesterday.getDate();
}

// 辅助函数：格式化时间为HH:mm
function formatTime(date) {
    return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
}

// 辅助函数：中文日期格式化
function formatChineseDate(date, includeYear) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    
    return includeYear 
        ? `${year}年${month}月${day}日`
        : `${month}月${day}日`;
}